//! @Author: DengLibin
//! @Date: Create in 2023-11-06 17:34:15
//! @Description: 

use std::fmt::Display;

use serde::{Deserialize, Serialize};

use crate::global::{GlobalError, GlobalResult};

#[derive(Debug, Serialize, Deserialize)]
pub struct AppConfig {
   pub port: u16, //端口
   pub mysql_url: String, //mysql连接
}


//转换结果
fn check_result<T, E: Display>(result: Result<T, E>) -> GlobalResult<T> {
    return match result {
        Ok(r) => Result::Ok(r),
        Err(err) => Result::Err(GlobalError {
            msg: format!("{}:{}", "[加载配置文件错误]", err.to_string())
        }),
    };
}

/// @Author: DengLibin
/// @Date: Create in 2023-11-01 16:21:13
/// @Description: 读取文件配置
pub fn read_yaml_config(file_path: &str) -> GlobalResult<AppConfig> {
    // 使用相对路径读取 app.yaml 内容为字符串
    // let yaml_str = read_file_str(file_path)?;
    let  file = check_result(std::fs::File::open(file_path))?;
    // serde_yaml 解析字符串为 User 对象
    let config: AppConfig = check_result(serde_yaml::from_reader(file))?;
    return Ok(config);
}
